package com.youlai.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.common.model.Option;
import com.youlai.system.converter.QuestionConverter;
import com.youlai.system.mapper.SysQuestionMapper;
import com.youlai.system.model.entity.SysQuestion;
import com.youlai.system.model.form.QuestionForm;
import com.youlai.system.model.query.QuestionPageQuery;
import com.youlai.system.model.vo.QuestionPageVO;
import com.youlai.system.service.SysQuestionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 问题分类业务处理实现
 */
@Service
@RequiredArgsConstructor
public class SysQuestionServiceImpl extends ServiceImpl<SysQuestionMapper, SysQuestion> implements SysQuestionService {

    private final QuestionConverter questionConverter;

    @Override
    public SysQuestion getByContentEqual(String content) {
        LambdaQueryWrapper<SysQuestion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysQuestion::getContent, content);
        return getOne(queryWrapper);
    }

    @Override
    public SysQuestion saveByContent(String content) {
        SysQuestion question = new SysQuestion();
        question.setContent(content);
        save(question);
        return question;
    }

    @Override
    public Page<QuestionPageVO> getQuestionPage(QuestionPageQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        String keywords = queryParams.getKeywords();

        LambdaQueryWrapper<SysQuestion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(keywords), SysQuestion::getContent, keywords);
        queryWrapper.orderByAsc(SysQuestion::getQuota);
        //查询数据
        Page<SysQuestion> questionPage = this.page(new Page<>(pageNum, pageSize), queryWrapper);
        return questionConverter.entity2Page(questionPage);
    }

    @Override
    public boolean saveQuestion(QuestionForm questionForm) {
        String content = questionForm.getContent();

        long count = this.count(new LambdaQueryWrapper<SysQuestion>().eq(SysQuestion::getContent, content));
        Assert.isTrue(count == 0, "问题分类已存在");

        // 实体转换
        SysQuestion question = questionConverter.form2Entity(questionForm);
        return save(question);
    }

    @Override
    public boolean updateQuestion(Long questionId, QuestionForm questionForm) {
        String content = questionForm.getContent();

        long count = this.count(new LambdaQueryWrapper<SysQuestion>()
                .eq(SysQuestion::getContent, content)
                .ne(SysQuestion::getId, questionId)
        );
        Assert.isTrue(count == 0, "问题分类已存在");

        // form -> entity
        SysQuestion entity = questionConverter.form2Entity(questionForm);

        // 修改问题分类
        return this.updateById(entity);
    }

    @Override
    public QuestionForm getQuestionForm(Long questionId) {
        SysQuestion entity = this.getById(questionId);
        return questionConverter.entity2Form(entity);
    }

    @Override
    public boolean deleteQuestions(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "问题分类删除数据为空");
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    @Override
    public boolean deleteQuestions(List<Long> idList) {
        return this.removeByIds(idList);
    }

    @Override
    public List<Option> listQuestionOptions(Integer quota) {
        LambdaQueryWrapper<SysQuestion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(quota != null, SysQuestion::getQuota, quota);
        queryWrapper.select(SysQuestion::getId, SysQuestion::getContent);
        queryWrapper.orderByAsc(SysQuestion::getSort);
        // 查询数据
        List<SysQuestion> questionList = this.list(queryWrapper);

        // 实体转换
        return questionConverter.entities2Options(questionList);
    }
}